package com.jeff.android.lab.jetpack.activity

import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.widget.ImageView
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.FileProvider
import butterknife.BindView
import butterknife.ButterKnife
import butterknife.OnClick
import com.jeff.android.lab.BaseActivity
import com.jeff.android.lab.R
import com.jeff.utils.ImageUtil
import com.jeff.utils.ToastUtil
import com.jeff.utils.security.MD5Util
import java.io.File

/**
 * @author: dingfeng
 * @date: 2021/3/2
 * @since: 1.0.0
 **/
class Activity_1_2 : BaseActivity() {

    @BindView(R.id.iv)
    lateinit var mImageView: ImageView

    var launcherActivityResult: ActivityResultLauncher<Intent>? = null

    var launcherPermission: ActivityResultLauncher<String>? = null

    var launcherPermissions: ActivityResultLauncher<Array<String>>? = null

    var launcherTakePicPreView: ActivityResultLauncher<Void>? = null

    var launcherTakePic: ActivityResultLauncher<Uri>? = null

    var launcherTakeVideo: ActivityResultLauncher<Uri>? = null

    var launcherPickerContact: ActivityResultLauncher<Void>? = null

    var launcherContent: ActivityResultLauncher<String>? = null

    var launcherMultipleContent: ActivityResultLauncher<String>? = null

    var launcherOpenDocument: ActivityResultLauncher<Array<String>>? = null

    var launcherOpenMultipleDocument: ActivityResultLauncher<Array<String>>? = null

    var launcherOpenDocumentTree: ActivityResultLauncher<Uri>? = null

    var launcherCreateDocument: ActivityResultLauncher<String>? = null

    private var mTakePicUri: Uri? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.jetpack_1_2_activity)
        ButterKnife.bind(this)

        launcherActivityResult = registerForActivityResult(
                ActivityResultContracts.StartActivityForResult()
        ) { activityResult: ActivityResult ->
            val data = activityResult.data
            ToastUtil.show(this, data?.getStringExtra("data"))
        }


        launcherPermission = registerForActivityResult(
                ActivityResultContracts.RequestPermission()
        ) { data: Boolean ->
            ToastUtil.show(this, data.toString())
        }

        launcherPermissions = registerForActivityResult(
                ActivityResultContracts.RequestMultiplePermissions()
        ) { data: Map<String, Boolean> ->
            ToastUtil.show(this, data.toString())
        }

        launcherTakePicPreView = registerForActivityResult(
                ActivityResultContracts.TakePicturePreview()
        ) { data: Bitmap? ->
            if (data != null)
                mImageView.setImageBitmap(data)
        }

        launcherTakePic = registerForActivityResult(
                ActivityResultContracts.TakePicture()
        ) { data: Boolean ->
            ToastUtil.show(this, data.toString())
            if (data)
                mImageView.setImageURI(mTakePicUri)
        }

        launcherTakeVideo = registerForActivityResult(
                ActivityResultContracts.TakeVideo()
        ) { data: Bitmap? ->
            if (data != null)
                mImageView.setImageBitmap(data)
        }

        launcherPickerContact = registerForActivityResult(
                ActivityResultContracts.PickContact()
        ) { data: Uri? ->
            ToastUtil.show(this, data?.toString())
        }

        launcherContent = registerForActivityResult(
                ActivityResultContracts.GetContent()
        ) { data: Uri? ->
            ToastUtil.show(this, data?.toString())
        }

        launcherMultipleContent = registerForActivityResult(
                ActivityResultContracts.GetMultipleContents()
        ) { data: List<Uri>? ->
            ToastUtil.show(this, data?.toString())
        }

        launcherOpenDocument = registerForActivityResult(
                ActivityResultContracts.OpenDocument()
        ) { data: Uri? ->
            ToastUtil.show(this, data?.toString())
        }

        launcherOpenMultipleDocument = registerForActivityResult(
                ActivityResultContracts.OpenMultipleDocuments()
        ) { data: List<Uri>? ->
            ToastUtil.show(this, data?.toString())
        }

        launcherOpenDocumentTree = registerForActivityResult(
                ActivityResultContracts.OpenDocumentTree()
        ) { data: Uri? ->
            ToastUtil.show(this, data?.toString())
        }

        launcherCreateDocument = registerForActivityResult(
                ActivityResultContracts.CreateDocument()
        ) { data: Uri? ->
            ToastUtil.show(this, data?.toString())
        }

    }

    @OnClick(R.id.btn_launch)
    fun launchActivityForResult() {
        val intent = Intent(this, Activity2::class.java)
        launcherActivityResult?.launch(intent)
    }


    @OnClick(R.id.btn_request_permission)
    fun openRequestPermission() {
        launcherPermission?.launch(android.Manifest.permission.CAMERA)
    }

    @OnClick(R.id.btn_request_permissions)
    fun openRequestPermissions() {
        launcherPermissions?.launch(arrayOf(android.Manifest.permission.CAMERA, android.Manifest.permission.READ_CONTACTS))
    }

    @OnClick(R.id.btn_take_picture_preview)
    fun openTakePicPreView() {
        launcherTakePicPreView?.launch(null)
    }

    @OnClick(R.id.btn_take_picture)
    fun openTakePic() {
        val cachePath = externalCacheDir?.absolutePath
        val path = cachePath + File.separator + MD5Util.get32MD5String(System.currentTimeMillis().toString()) + ImageUtil.JPG
        mTakePicUri = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            Uri.fromFile(File(path))
        } else {
            FileProvider.getUriForFile(this, "$packageName.fileprovider", File(path))
        }
        launcherTakePic?.launch(mTakePicUri)
    }

    @OnClick(R.id.btn_take_video)
    fun openTakeVideo() {
        val cachePath = externalCacheDir?.absolutePath
        val path = cachePath + File.separator + MD5Util.get32MD5String(System.currentTimeMillis().toString()) + ".mp4"
        val uri = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            Uri.fromFile(File(path))
        } else {
            FileProvider.getUriForFile(this, "$packageName.fileprovider", File(path))
        }
        launcherTakeVideo?.launch(uri)
    }

    @OnClick(R.id.btn_pick_contact)
    fun openPickContact() {
        launcherPickerContact?.launch(null)
    }

    @OnClick(R.id.btn_get_content)
    fun openGetContent() {
        launcherContent?.launch("*/*")
    }

    @OnClick(R.id.btn_get_multiple_content)
    fun openPickMultipleContact() {
        launcherMultipleContent?.launch("*/*")
    }

    @OnClick(R.id.btn_open_document)
    fun openDocument() {
        launcherOpenDocument?.launch(null)
    }

    @OnClick(R.id.btn_open_multiple_document)
    fun openMultipleDocument() {
        launcherOpenMultipleDocument?.launch(arrayOf("*/*"))
    }

    @OnClick(R.id.btn_open_document_tree)
    fun openDocumentTree() {
        launcherOpenDocumentTree?.launch(Uri.parse("content://com.android.providers.media.documents/document/image:11102"))
    }

    @OnClick(R.id.btn_create_document)
    fun createDocument() {
        launcherCreateDocument?.launch("test")
    }

}